//
//  PurumClothesViewController.m
//  PurumClothes
//
//  Created by 솔이 박 on 11. 4. 4..
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "PurumClothesViewController.h"
#import "PurumMinigameDefinition.h"

@implementation PurumClothesViewController

// ** main view
@synthesize mainView;
@synthesize wearClothView;

// ** menu view
@synthesize menuView;
@synthesize clothButton;
@synthesize accessoryButton;
@synthesize miniGameButton;

// ** item view
@synthesize itemView;
@synthesize returnButton;
@synthesize itemListView;


// ** mini game view
@synthesize miniGameView;
@synthesize prGundamImage;
@synthesize moveAreaView;
@synthesize redBulletButton;
@synthesize yellowBulletButton;
@synthesize blueBulletButton;

- (IBAction) miniGameButtonPressed
{
	viewType = ViewType_MiniGame;
	
	[mainView setHidden:YES];
	[menuView setHidden:YES];
	[itemView setHidden:YES];
	[miniGameView setHidden:NO];
}



- (IBAction) clothButtonPressed
{
	viewType = ViewType_ClothItem;
	
	[mainView setHidden:NO];
	[menuView setHidden:YES];
	[itemView setHidden:NO];
	[miniGameView setHidden:YES];
	
	[self initializeClothItem];
}

- (IBAction) accessoryButtonPressed
{
	viewType = ViewType_AccessoryItem;
	
	[mainView setHidden:NO];
	[menuView setHidden:YES];
	[itemView setHidden:NO];
	[miniGameView setHidden:YES];
	
	[self initializeAccessoryItem];
}

- (IBAction) returnButtonPressed
{	
	viewType = ViewType_Menu;
	
	[mainView setHidden:NO];
	[menuView setHidden:NO];
	[itemView setHidden:YES];
	[miniGameView setHidden:YES];
}

- (void) initializeFaceItem
{	
	
	int size = 0;
	
	faceTypes[size++] = @"face_smile.png";
	faceTypes[size++] = @"face_sad.png";
	
	[self initializeItem:faceTypes withSize:size];
}

- (void) initializeClothItem
{
	
	int size = 0;
	
	clothTypes[size++] = @"cloth_winter.png";
	clothTypes[size++] = @"cloth_summer.png";
	clothTypes[size++] = @"cloth_suit.png";
	clothTypes[size++] = @"cloth_hanbok.png";
	clothTypes[size++] = @"cloth_girl.png";
	clothTypes[size++] = @"cloth_ballet.png";
	clothTypes[size++] = @"cloth_gundam.png";
	
	[self initializeItem:clothTypes withSize:size];	
}

- (void) initializeAccessoryItem
{
	
	int size = 0;
	
	accessoryTypes[size++] = @"acce_cat.png";
	accessoryTypes[size++] = @"acce_flower.png";
	accessoryTypes[size++] = @"acce_glasses.png";
	accessoryTypes[size++] = @"acce_got.png";
	accessoryTypes[size++] = @"acce_gundam.png";
	accessoryTypes[size++] = @"acce_muffler.png";
	accessoryTypes[size++] = @"acce_pin.png";
	accessoryTypes[size++] = @"acce_wing.png";
	
	[self initializeItem:accessoryTypes withSize:size];
}

- (void) initializeItem : (NSString**)types withSize :(int) size
{
	int total_width = 0;
	
	if(!itemButtons[0])
	{
		itemButtons[0] = [UIButton buttonWithType:UIButtonTypeRoundedRect];
		
		[itemButtons[0] setTitle:@"nothing" forState:UIControlStateNormal];
		
		CGRect frame = itemButtons[0].frame;	
		frame.origin = CGPointMake(total_width+20, 20);
		frame.size = CGSizeMake(100, 160);
		itemButtons[0].frame = frame;
		
		itemButtons[0].contentMode = UIViewContentModeScaleAspectFit;
		
		[itemButtons[0] addTarget:self action:@selector(itemButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
		itemButtons[0].tag = (-1);			
		
		[itemListView addSubview:itemButtons[0]];			
		
	}
	
	CGRect frame = itemButtons[0].frame;			
	total_width += (frame.size.width+20);
	

	
	for(int index=1; index<10; ++index)
	{
		if(itemButtons[index])
		{
			[itemButtons[index] removeFromSuperview];
			itemButtons[index] = 0;
		}
	
		if(index<=size)
		{
			itemButtons[index] = [UIButton buttonWithType: UIButtonTypeCustom];
			
			UIImage *image = [UIImage imageNamed:types[index-1]];
			
			[itemButtons[index] setImage:image forState:UIControlStateNormal];
			
			CGRect frame = itemButtons[index].frame;	
			frame.origin = CGPointMake(total_width+20, 20);
			frame.size = CGSizeMake(image.size.width, 160);
			itemButtons[index].frame = frame;
			
			itemButtons[index].contentMode = UIViewContentModeScaleAspectFit;
			
			total_width += (frame.size.width+20);
			
			[itemButtons[index] addTarget:self action:@selector(itemButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
			itemButtons[index].tag = (index-1);			
			
			[itemListView addSubview:itemButtons[index]];			
		}
		
	}

	CGSize contentSize = itemListView.contentSize;
	
	contentSize.width = total_width - 460;
	if(contentSize.width < 0)
	{
		contentSize.width = 0;
	}

	itemListView.contentSize = contentSize;
}


- (void) itemButtonPressed :(id)sender
{
	 int index = ((UIControl *) sender).tag;
	
	switch(viewType)
	{
		case ViewType_ClothItem:
		{
			[self wearCloth:index];
		}
		break;
		case ViewType_AccessoryItem:
		{
			[self wearAccessory:index];
		}
		break;
	}
}

- (void) wearCloth:(int)index
{
	if(wearClothImage)
	{
		[wearClothImage removeFromSuperview];
		wearClothImage = 0;
	}
	
	if(index < 0)
	{
		return;
	}
		
	UIImage *image = [UIImage imageNamed:clothTypes[index]];
	
	wearClothImage = [[[UIImageView alloc] initWithImage:image]autorelease];

	CGRect frame = wearClothImage.frame;
	frame.size = CGSizeMake(frame.size.width, 110);
	frame.origin = CGPointMake(251 - frame.size.width/2, 251 - frame.size.height/2);
	wearClothImage.frame = frame;
	
	wearClothImage.contentMode = UIViewContentModeScaleAspectFit;
	
	[self.wearClothView addSubview:wearClothImage];
	
}

- (void) wearAccessory:(int)index
{	
	if(index < 0)
	{
		return;
	}
	
	int wearAccessoryTag = (100+index);
	
	UIImageView *wearAccessoryView = (UIImageView*)[self.mainView viewWithTag:wearAccessoryTag];
	if(wearAccessoryView)
	{
		[wearAccessoryView setHidden:(!wearAccessoryView.hidden)];
	}
}

/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/





// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight
			);
}


- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
	
    UITouch *touch = [touches anyObject];
	
    if(touch.view == self.moveAreaView)
    {
		moveTouchOffset = prGundamImage.center.y - [touch locationInView:touch.view].y;
    }
	
}

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{    
	UITouch *touch = [touches anyObject];
	
    if(touch.view == self.moveAreaView)
    {
		float distanceMoved = ([touch locationInView:touch.view].y + moveTouchOffset) - prGundamImage.center.y;
		float newY = prGundamImage.center.y + distanceMoved;
		
		if(newY>75 && newY<245)
			prGundamImage.center = CGPointMake(prGundamImage.center.x, newY);
		
		if(newY>245)
			prGundamImage.center = CGPointMake(prGundamImage.center.x, 245);
		
		if(newY<75)
			prGundamImage.center = CGPointMake(prGundamImage.center.x, 75);		
    }
}


- (IBAction)redBulletButtonPressed
{
	[self shotBullet:BulletType_Red];
}

- (IBAction)yellowBulletButtonPressed
{
	[self shotBullet:BulletType_Yellow];
}

- (IBAction)blueBulletButtonPressed
{
	[self shotBullet:BulletType_Blue];
}

- (void)shotBullet:(BulletType)type
{
	PurumMinigameBullet *bullet = [PurumMinigameBullet alloc];
	
	int startX = prGundamImage.center.x;
	int startY = prGundamImage.center.y;
	
	[bullet initialize:miniGameView ofBulletType:type withStartX:startX andStartY:startY];
	
//	[bulletArray addObject:bullet];
	
}

- (void)dealloc {
    [super dealloc];
}

@end
